package com.bnlr.lrparking.appui.paypwd;

import android.os.Bundle;
import android.widget.TextView;

import com.bnlr.lrparking.app.Application;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.app.AppConfig;
import com.bnlr.lrparking.constant.LrConstant;
import com.bnlr.lrparking.constant.Urls;
import com.bnlr.lrparking.model.VerifyCodeM;
import com.bnlr.lrparking.util.CountDownTimerUtils;
import com.bnlr.lrparking.view.PasswordView;
import com.bnlr.lrparking.util.CommonUtil;
import com.bnlr.nohttp.CallServer;
import com.bnlr.nohttp.CustomHttpListener;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.Request;

import org.json.JSONObject;

/**
 * Created by hp on 2017/8/22.
 * 忘记支付密码
 */

public class ForgetPayPwdActivity extends BaseActivity {

    private TextView tvNavTitle;
    private TextView phone;
    private TextView forget_pwd_verific;
    private PasswordView passwordView;
    private String mobile = "";
    private String verific = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forgetpaypwd);
        initView();
    }

    private void initView() {
        tvNavTitle = (TextView) findViewById(R.id.tv_nav_title);
        tvNavTitle.setText("重置支付密码");
        passwordView = (PasswordView) findViewById(R.id.passwordView);
        passwordView.setCipherEnable(false);
        phone = (TextView) findViewById(R.id.forget_phone);
        forget_pwd_verific = (TextView) findViewById(R.id.forget_pwd_verific);
        mobile = AppConfig.getInstance().getString("userName", "");
        String maskNumber = mobile.substring(0, 3) + "****" + mobile.substring(7, mobile.length());
        phone.setText(maskNumber);
        SendYzm();

        passwordView.setPasswordListener(new PasswordView.PasswordListener() {
            @Override
            public void passwordChange(String changeText) {
            }

            @Override
            public void passwordComplete() {
                if (verific.equals(passwordView.getPassword())) {
                    startActivity(ForgetPwdAuthenticationActivity.class);
                }
            }

            @Override
            public void keyEnterPress(String password, boolean isComplete) {
            }
        });
    }

    /**
     * 验证码请求
     */
    public Request<String> mRequest;

    private void SendYzm() {
        try {
            mRequest = NoHttp.createStringRequest(Urls.IP, LrConstant.POST);
            mRequest.add("action", "sendValidateMCD");
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("phone", mobile);
            mRequest.add("flag", "0");
            // 添加到请求队列
            CallServer.getRequestInstance().add(ForgetPayPwdActivity.this, mRequest,
                    new CustomHttpListener<VerifyCodeM>(ForgetPayPwdActivity.this, true, VerifyCodeM.class) {
                        @Override
                        public void doWork(VerifyCodeM data, String code) {
                        }

                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {

                            try {//{"msgcode":"1","msg":"成功","data":{"code":"06641"}}
                                super.onFinally(obj, code, isSucceed);
                                if ("1".equals(code)) {
                                    setVerifica(forget_pwd_verific);
                                    verific = obj.getJSONObject("data").getString("code");
                                } else {
                                    CommonUtil.showToask(ForgetPayPwdActivity.this, obj.getString("msg"));
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }, true);
        } catch (Exception e) {

        }
    }

    private void setVerifica(TextView verifica) {
        CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(verifica, 60000, 1000);
        mCountDownTimerUtils.start();
    }
}
